<template>
  <div>
    <van-nav-bar
      :title="
        comment.reply_count > 0 ? `${comment.reply_count}条回复` : '暂无回复'
      "
    >
      <van-icon slot="left" name="cross" @click="$emit('close')" />
    </van-nav-bar>

    <div class="scroll-wrap">
      <!-- 当前评论 -->
      <comment-item :comment="comment" />

      <van-cell title="全部回复" />

      <!-- 评论回复列表 -->
      <comment-list :source="comment.com_id" type="c" :list="commentList" />
    </div>

    <!-- 发布评论回复 -->
    <div class="reply-wrap">
      <van-button
        class="post-btn"
        size="small"
        text="写评论"
        round
        @click="isPostCommentShow = true"
      />
    </div>

    <!-- 发布评论弹出层 -->
    <van-popup v-model="isPostCommentShow" position="bottom">
      <comment-post :target="comment.com_id" @postSuccess="onReplySuccess" />
    </van-popup>
  </div>
</template>

<script>
import CommentItem from './CommentItem'
import CommentList from './CommentList'
import CommentPost from './CommentPost'

export default {
  name: 'CommentReply',
  components: {
    CommentItem,
    CommentList,
    CommentPost
  },
  props: {
    comment: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      isPostCommentShow: false,
      commentList: []
    }
  },
  methods: {
    onReplySuccess(data) {
      // 更新回复的数量
      this.comment.reply_count++
      // 关闭回复评论弹框
      this.isPostCommentShow = false
      // 追加最新评论到列表顶部
      this.commentList.unshift(data.new_obj)
    }
  }
}
</script>

<style lang="less" scoped>
.scroll-wrap {
  position: fixed;
  top: 92px;
  left: 0;
  right: 0;
  bottom: 88px;
  overflow-y: auto;
}

.reply-wrap {
  position: fixed;
  right: 0;
  left: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 88px;
  border-top: 1px solid #ededed;
  background-color: #fff;

  .post-btn {
    width: 60%;
  }
}
</style>
